import { message } from 'antd';
import html2Canvas from 'html2canvas';
import jsPDF from 'jspdf';
import { getSystemType } from './authority';
/**
 * 导出为PDF
 */
const exportPDF = (domId: string, reloadCallback: () => void, pdfName?: string) => {
  if (domId) {
    message.success('文件生成中，请稍后...', 4);
    // const obj: HTMLElement  | null = document.getElementById(domId)?.width(1000 + "px")
    const domInfo: any = document.getElementById(domId);
    html2Canvas(domInfo, {
      scale: 2,
      width: 1000,
      // height: 700,
      // logging: true,
      // devicePixelRatio: 1.25,
    }).then((canvas) => {
      var contentWidth = canvas.width;
      var contentHeight = canvas.height;
      var pageHeight = (contentWidth / 592.28) * 841.89;
      var leftHeight = contentHeight;
      //页面偏移
      var position = 0;
      //a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
      var imgWidth = 595.28;
      var imgHeight = (595.28 / contentWidth) * contentHeight;

      var pageData = canvas.toDataURL('image/jpeg', 1.0);
      var pdf = new jsPDF(undefined, 'pt', 'a4');

      //放大会清晰一点
      pdf.internal.scaleFactor = 1.33;
      //有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
      //当内容未超过pdf一页显示的范围，无需分页
      if (leftHeight < pageHeight) {
        pdf.addImage(pageData, 'JPEG', 20, 40, imgWidth, imgHeight);
      } else {
        while (leftHeight > 0) {
          pdf.addImage(pageData, 'JPEG', 20, position + 40, imgWidth, imgHeight);
          leftHeight -= pageHeight;
          position -= 841.89;
          //避免添加空白页
          if (leftHeight > 0) {
            pdf.addPage();
          }
        }
      }
      pdf.save(`${pdfName ? pdfName : '审核记录'}.pdf`);
      reloadCallback();
    });
  }
};

/** 判断系统类别，没有默认为 0 */
const getSystemTypeValue = () => {
  const systemValue = getSystemType();
  if (systemValue == '0' || systemValue == '1' || systemValue == '2' || systemValue == '3' || systemValue == '4') {
    return systemValue;
  } else {
    return '0';
  }
};

export { exportPDF, getSystemTypeValue };
